package cn.iocoder.yudao.module.system.controller.admin.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.scores.*;
import cn.iocoder.yudao.module.system.dal.dataobject.user.UserScoresDO;
import cn.iocoder.yudao.module.system.service.user.UserScoresService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户分数")
@RestController
@RequestMapping("/system/user-scores")
@Validated
public class UserScoresController {

    @Resource
    private UserScoresService userScoresService;

    @PostMapping("/create")
    @Operation(summary = "创建用户分数")
    public CommonResult<Long> createUserScores(@Valid @RequestBody UserScoresSaveReqVO createReqVO) {
        return success(userScoresService.createUserScore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户分数")
    public CommonResult<Boolean> updateUserScores(@Valid @RequestBody UserScoresSaveReqVO updateReqVO) {
        userScoresService.updateUserScore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户分数")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUserScores(@RequestParam("id") Long id) {
        userScoresService.deleteUserScore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户分数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserScoresRespVO> getUserScores(@RequestParam("id") Long id) {
        UserScoresDO userScores = userScoresService.getUserScore(id);
        return success(BeanUtils.toBean(userScores, UserScoresRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户分数分页")
    public CommonResult<PageResult<UserScoresRespVO>> getUserScoresPage(@Valid UserScoresPageReqVO pageReqVO) {
        PageResult<UserScoresDO> pageResult = userScoresService.getUserScorePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserScoresRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户分数 Excel")
    public void exportUserScoresExcel(@Valid UserScoresExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<UserScoresDO> list = userScoresService.getUserScoreList(exportReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "用户分数.xls", "数据", UserScoresRespVO.class,
                BeanUtils.toBean(list, UserScoresRespVO.class));
    }

    @GetMapping("/list-by-user")
    @Operation(summary = "获得指定用户的分数列表")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1024")
    public CommonResult<List<UserScoresRespVO>> getUserScoresByUserId(@RequestParam("userId") Long userId) {
        List<UserScoresDO> list = userScoresService.getUserScoresByUserId(userId);
        return success(BeanUtils.toBean(list, UserScoresRespVO.class));
    }

    @GetMapping("/current")
    @Operation(summary = "获得用户当前使用的分数记录")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1024")
    public CommonResult<UserScoresRespVO> getCurrentUserScore(@RequestParam("userId") Long userId) {
        UserScoresDO userScore = userScoresService.getCurrentUserScore(userId);
        return success(BeanUtils.toBean(userScore, UserScoresRespVO.class));
    }

    @PostMapping("/set-current")
    @Operation(summary = "设置用户当前使用的分数记录")
    @Parameter(name = "id", description = "分数记录编号", required = true, example = "1024")
    public CommonResult<Boolean> setCurrentUserScore(@RequestParam("id") Long id) {
        userScoresService.setCurrentUserScore(id);
        return success(true);
    }

    @PostMapping("/migrate-from-users")
    @Operation(summary = "从用户表迁移分数数据")
    public CommonResult<Boolean> migrateScoresFromUsers() {
        userScoresService.migrateScoresFromUsers();
        return success(true);
    }

}